package com.gm.wholesale.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.interfaces.INumberInputViewListener;
import com.gm.wholesale.util.AppUtil;

public class NumberInputView extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private TextView oneText1;
    private TextView oneText2;
    private TextView oneText3;
    private TextView deleteAllTv;
    private LinearLayout deleteOneView;
    private TextView towText1;
    private TextView towText2;
    private TextView towText3;
    private TextView threeText1;
    private TextView threeText2;
    private TextView threeText3;
    private TextView fourText1;
    private TextView fourText2;
    private SuperTextView subMitView;
    private EditText mEditText;



    private INumberInputViewListener iNumberInputViewListener;

    public void setINumberView(INumberInputViewListener iNumberInputViewListener) {
        this.iNumberInputViewListener = iNumberInputViewListener;
    }

    public NumberInputView(Context context) {
        super(context);
        initView(context);
    }

    public NumberInputView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
       LayoutInflater.from(context).inflate(R.layout.view_number_input, this);
        oneText1 = findViewById(R.id.oneText1);
        oneText2 = findViewById(R.id.oneText2);
        oneText3 = findViewById(R.id.oneText3);
        deleteAllTv = findViewById(R.id.deleteAllTv);
        deleteOneView = findViewById(R.id.deleteOneView);
        towText1 = findViewById(R.id.towText1);
        towText2 = findViewById(R.id.towText2);
        towText3 = findViewById(R.id.towText3);
        threeText1 = findViewById(R.id.threeText1);
        threeText2 = findViewById(R.id.threeText2);
        threeText3 = findViewById(R.id.threeText3);
        fourText1 = findViewById(R.id.fourText1);
        fourText2 = findViewById(R.id.fourText2);
        subMitView = findViewById(R.id.subMitView);
        oneText1.setOnClickListener(this);
        oneText2.setOnClickListener(this);
        oneText3.setOnClickListener(this);
        deleteAllTv.setOnClickListener(this);
        deleteOneView.setOnClickListener(this);
        towText1.setOnClickListener(this);
        towText2.setOnClickListener(this);
        towText3.setOnClickListener(this);
        threeText1.setOnClickListener(this);
        threeText2.setOnClickListener(this);
        threeText3.setOnClickListener(this);
        fourText1.setOnClickListener(this);
        fourText2.setOnClickListener(this);
        subMitView.setOnClickListener(this);
    }

    public void bindEdiText(EditText editText, String text) {
        AppUtil.hideSoftKeyboard(mContext,editText);
        editText.setSelection(0);
        if (!TextUtils.isEmpty(text)){
            editText.setText(text);
            editText.setSelection(text.length());
        }
        editText.setShowSoftInputOnFocus(false);
        editText.getViewTreeObserver().addOnGlobalLayoutListener(() -> {

            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

        });


        inputDataBuilder = new StringBuilder("");
        mEditText = editText;
        inputDataBuilder.append(text);

    }

    public EditText getBindEd(){
        return mEditText;
    }


    private StringBuilder inputDataBuilder = new StringBuilder("");

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.oneText1) { //7
            addNum(7);
        } else if (id == R.id.oneText2) { //8
            addNum(8);
        } else if (id == R.id.oneText3) {//9
            addNum(9);
        } else if (id == R.id.deleteAllTv) {//删除
            cleanData();
        } else if (id == R.id.deleteOneView) {//删除一个
            deleteNum();
        } else if (id == R.id.towText1) {//4
            addNum(4);
        } else if (id == R.id.towText2) {//5
            addNum(5);
        } else if (id == R.id.towText3) {//6
            addNum(6);
        } else if (id == R.id.threeText1) {//1
            addNum(1);
        } else if (id == R.id.threeText2) {//2
            addNum(2);
        } else if (id == R.id.threeText3) {//3
            addNum(3);
        } else if (id == R.id.fourText1) {//0
            addNum(0);
        } else if (id == R.id.fourText2) {//.
            addDrop();
        } else if (id == R.id.subMitView) {//提交
            if (iNumberInputViewListener != null) {
                iNumberInputViewListener.onSubmit();
            }
        }

    }


    private void showAddEditText() {
            if (mEditText != null) {
                int selectPos = mEditText.getSelectionStart();
                String inputData = getData();
                if (TextUtils.isEmpty(inputData)) {
                    mEditText.setText("");
                    return;
                }
                mEditText.setText(inputData);
                setEdSelection(mEditText,selectPos + 1);
            }


    }

    private void addNum(int num) {
        try{
            if (mEditText != null) {
                int selectPos = mEditText.getSelectionStart();
                String tem=getInputDataBuilder().toString();
                if (!TextUtils.isEmpty(tem)){
                    int tag=tem.indexOf(".");
                    if (tag>0){
                        String tem2=tem.substring(tag);
                        if (tem2.length()>=3
                                && selectPos>tag){
                            ToastUtil.showToast("最多输入2位小数");
                            return;
                        }
                    }
                }
                getInputDataBuilder().insert(selectPos, num);
                showAddEditText();
            }
        }
        catch (Exception e){

        }


    }

    private StringBuilder getInputDataBuilder() {
        String text = mEditText.getText().toString();
        inputDataBuilder = new StringBuilder(text);
        return inputDataBuilder;
    }


    private void deleteNum() {
        if (mEditText != null) {
            int pos = mEditText.getSelectionStart();
            if (pos <= 0) {
                return;
            }
            int selectPos = pos - 1;
            getInputDataBuilder().deleteCharAt(selectPos);
            mEditText.setText(getData());
            setEdSelection(mEditText,selectPos);

        }

    }

    private void cleanData() {
            inputDataBuilder = new StringBuilder("");
            if (mEditText!=null){
                mEditText.setText("");
                mEditText.setSelection(0);
            }


    }

    private void setEdSelection(EditText editText,int pos){
        try {
            if (editText!=null){
                editText.setSelection(pos);
            }
        }
        catch (Exception e){
        }

    }

    private void addDrop() {
        try {
            String inputData = getData();
            if (!TextUtils.isEmpty(inputData)
                    && !inputData.contains(".")) {
                int pos = mEditText.getSelectionStart();

                String tem=getInputDataBuilder().toString();
                if (pos<(tem.length()-2)){
                    return;
                }
                getInputDataBuilder().insert(pos, ".");
                showAddEditText();
            }
        }
        catch (Exception e){

        }

    }



    private String getData() {
        return inputDataBuilder.toString().trim();
    }
}
